<template>
	<div id="appRoot">
		<sidebar>
			<card :label="pageTitle">
				<div class="local-card-header">
					<el-button v-if="isCollection" type="primary" size="small" @click="collectionBatch">提交到待审核</el-button>
					<el-button v-else type="primary" size="small" @click="batch">提交审核</el-button>
					<el-button type="text" size="small" @click="goback">返回上一级</el-button>
				</div>
			</card>
			<div class="table-container" v-loading.body="loading">
				<template v-if="tableDataError">
					<table-data-fail type="error"></table-data-fail>
				</template>
				<template v-else-if="tableData.length > 0">
					<el-table
							:data="tableData"
							ref="selectTable"
							stripe
							border
							style="width: 100%"
							@select="handleSelect"
							@select-all="handleSelectall">
						<el-table-column
								type="selection"
								width="55">
						</el-table-column>
						<el-table-column
								prop="original"
								align="center"
								header-align="left"
								label="原文">
						</el-table-column>
						<el-table-column
								align="center"
								header-align="left"
								label="指标">
							<template scope="scope">
								<el-input v-model="scope.row.labelName" auto-complete="off"></el-input>
							</template>
						</el-table-column>
						<el-table-column
								align="center"
								header-align="left"
								label="数量">
							<template scope="scope">
								<el-input v-model="scope.row.dataNum" auto-complete="off"></el-input>
							</template>
						</el-table-column>
						<el-table-column
								align="center"
								header-align="left"
								label="单位">
							<template scope="scope">
								<el-input v-model="scope.row.dataUnit" auto-complete="off"></el-input>
							</template>
						</el-table-column>
						<el-table-column
								prop="orgName"
								align="center"
								header-align="left"
								label="地区">
						</el-table-column>
						<el-table-column
								align="center"
								header-align="left"
								label="时间">
							<template scope="scope">
								<el-input v-model="scope.row.dataTime" auto-complete="off"></el-input>
							</template>
						</el-table-column>
						<el-table-column
								prop="state"
								align="center"
								header-align="left"
								label="指标状态">
						</el-table-column>
						<el-table-column
								prop="businessName"
								align="center"
								header-align="left"
								label="指标类型">
							<template scope="scope">
								<el-select v-model="scope.row.businessId" placeholder="请选择数据出处">
									<el-option v-for="item in businessConfig" :label="item.busiName" :value="item.busiId" :key="item.busiId"></el-option>
								</el-select>
							</template>
						</el-table-column>
						<el-table-column
								align="center"
								header-align="left"
								label="隶属部门">
							<template scope="scope">
								<el-select v-model="scope.row.tradeId" placeholder="请选择隶属部门">
									<el-option v-for="item in tradeConfig" :label="item.tradeName" :value="item.tradeId" :key="item.tradeId"></el-option>
								</el-select>
							</template>
						</el-table-column>
						<el-table-column
								align="center"
								header-align="left"
								label="操作">
							<template scope="scope">
								<el-button type="text" size="small" @click="copyData(scope.row.dataId, scope.$index)">复制</el-button>
								<el-button type="text" size="small" @click="saveData(scope.row, true)">保存</el-button>
							</template>
						</el-table-column>
					</el-table>
					<div class="pagination-wrapper">
						<el-pagination
								@current-change="getTableData"
								:current-page.sync="paginationData.currentPage"
								:page-size="paginationData.pageSize"
								layout="prev, pager, next, jumper"
								:total="paginationData.total">
						</el-pagination>
					</div>
				</template>
				<template v-else>
					<table-data-fail type="empty"></table-data-fail>
				</template>
			</div>
		</sidebar>
	</div>
</template>

<script>
	import sidebar from 'components/layout/sidebar.vue'
	import card from 'components/layout/card.vue'
	import tableDataFail from 'components/layout/tableDataFail.vue'
    import ajax from 'common/ajax'
    import ajaxUrls from 'common/ajaxUrls'
	import getUrlPara from 'common/getUrlPara'
    import {qryBusinessConfig, qryTradeConfig} from 'common/unitQry'
	export default{
		data(){
			return{
			    id: '',
                batchNo: '',
				loading: true,
				paginationData:{
					currentPage: 1,
					pageSize: 10,
					total: 0
				},
				tableDataError: false,
				tableData: [],
                tradeConfig: [],
                businessConfig: [],
				selectedData: [],
                isCollection: '',
				pageTitle: '',
			}
		},
        mounted(){
            qryBusinessConfig().then(data => {
                this.businessConfig = data.data.items || [];
            });
            qryTradeConfig().then(data => {
                this.tradeConfig = data.data.items || [];
            });
		    this.id = getUrlPara('id');
            this.batchNo = getUrlPara('batchNo');
            this.isCollection = getUrlPara('isCollection');
            if(this.isCollection){
                this.pageTitle = '采集数据';
			}else{
                this.pageTitle = '入库审核';
			}
            this.getTableData();
        },
		methods: {
            handleSelect(selection, row){
                let isSave = 0;
                selection.forEach( item => {
                    if(item.dataId === row.dataId){
                        isSave = 1;
					}
				});
                row.isSave = isSave;
                this.saveData(row);
			},
            handleSelectall(selection){
                let isSave = 0;
                if(selection.length > 0){
                    isSave = 1
				}
                this.tableData.forEach( item => {
                    item.isSave = isSave;
                    this.saveData(item);
                })
			},
			setSelectedData(){
                this.$nextTick(()=>{
                    let rows = this.tableData.filter(item => {
                        return item.isSave === 1
                    });
                    if (rows.length>0) {
                        rows.forEach(row => {
                            this.$refs.selectTable.toggleRowSelection(row);
                        });
                    } else {
                        this.$refs.selectTable.clearSelection();
                    }
				})
			},
            batch(){
                ajax({
                    method: 'post',
                    url: ajaxUrls.apiUrl.FileConfig,
                    action: 'auditFile',
                    data: {
                        batchNo: this.batchNo,
                        fileId: this.id
					}
                }).then(function (data) {
                    if(data.resultCode == 0){
                        this.$alert('审核成功', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                location.replace('/storageExamine.html')
                            }
                        });
					}
                }.bind(this)).catch(function (err) {

                })
			},
            collectionBatch(){
                ajax({
                    method: 'post',
                    url: ajaxUrls.apiUrl.FileConfig,
                    action: 'submitAudit',
                    data: {
                        batchNo: this.batchNo,
                        fileId: this.id
                    }
                }).then(function (data) {
                    if(data.resultCode == 0){
                        this.$alert('已提交待审核', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                location.replace('/collection.html')
                            }
                        });
                    }
                }.bind(this)).catch(function (err) {

                })
			},
            goback(){
                history.go(-1)
			},
            copyData(dataId, index){
                ajax({
                    method: 'post',
                    url: ajaxUrls.apiUrl.TempDataAnalyzeAction,
                    action: 'copyTempDataByBatch',
                    data: {dataId: dataId}
                }).then(function (data) {

                    if(data.resultCode != 0){
                        this.$alert('系统错误，请联系维护人员处理', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
//                                location.reload();
                            }
                        });
                    }else{
                        let dataId = data.data.dataId;
                        let newData = Object.assign({},this.tableData[index], {dataId: dataId});
                        this.tableData.splice(index+1, 0, newData)
                    }
                }.bind(this)).catch(function (err) {

                })
			},
            saveData(oriData, isDailog){
                let data = {
                    batchNo: this.batchNo,
                    businessId: oriData.businessId,
                    dataId: oriData.dataId,
                    dataNum: oriData.dataNum,
                    dataTime: oriData.dataTime,
                    dataUnit: oriData.dataUnit,
                    isCopy: oriData.isCopy,
                    isLabel: oriData.isLabel,
                    isSave: oriData.isSave,
                    labelCode: oriData.labelCode || '',
                    labelId: oriData.labelId,
                    labelName: oriData.labelName,
                    orgId: oriData.orgId,
                    orgName: oriData.orgName,
                    originalText: oriData.original,
                    sortShow: oriData.sortShow,
                    tradeId: oriData.tradeId,
                    type: oriData.isCopy,
				};
                ajax({
                    method: 'post',
                    url: ajaxUrls.apiUrl.TempDataAnalyzeAction,
                    action: 'auditTempDataByBatch',
                    data: data
                }).then(function (data) {
                    if(isDailog){
                        this.$message({
                            message: '保存成功',
                            type: 'success'
                        });
					}
                }.bind(this)).catch(function (err) {

                })
			},
            getTableData(){
                this.loading = true;
                let data = {
                    pageNum: this.paginationData.currentPage,
                    pageRow: this.paginationData.pageSize
                };
                data = Object.assign(data, this.queryData, {batchNo: this.batchNo});
                ajax({
                    method: 'post',
                    url: ajaxUrls.apiUrl.TempDataAnalyzeAction,
                    action: 'qryTempDataByBatch',
                    data: data
                }).then(function (data) {
                    this.tableData = data.data.items || [];
                    this.paginationData.total = data.data.totalNum || 0;
					this.loading = false;
                    this.setSelectedData();
                }.bind(this)).catch(function (err) {

                })
            },
		},
		components:{
			sidebar,
			card,
			tableDataFail
		}
	}
</script>

<style lang="scss">
	.text-title{
		padding: 10px;
		font-size: 14px;
	}
	.local-card-header{
		padding: 10px;
	}
</style>